package com.distributer.settlement.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.distributer.settlement.comsumer.ConsumerHandler;
import com.distributer.settlement.configuration.KafkaConsumerConfig;

@Component
public class StartKafkaConsumerHandlerThread implements ApplicationListener<ContextRefreshedEvent>{

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerHandler.class);
	
	@Autowired
	private KafkaConsumerConfig kConsumerConfig;

	public void onApplicationEvent(ContextRefreshedEvent event) {
		final Thread initConsumerThread = new Thread(
				new ConsumerHandler(kConsumerConfig.getBootstrapServers(), "settlement-group", "settlement-topic",kConsumerConfig)
				,"initConsumer-Thread");
		initConsumerThread.start();
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			public void run() {
				initConsumerThread.interrupt();
				LOGGER.info("线程：" + initConsumerThread.getName() + "被中断！");
			}
		}));
	}

}
